from PyQt4 import QtGui, QtCore;

class UndockedWin(QtGui.QMainWindow):
    def __init__(self, dockWidget=None, parent=None):
        QtGui.QMainWindow.__init__(self, parent.mainWin);
        self.resize(400, 200);
        self.mainWin = parent.mainWin;
        self.dockWidget = dockWidget;
        
    def showEvent(self, event):
        super(UndockedWin, self).showEvent(event);
        self.dockWidget.bUndocked = True;
    
    def closeEvent(self, event):
        super(UndockedWin, self).closeEvent(event);
        self.restoreDocked();
        
    def restoreDocked(self):
        if self.dockWidget.bCentralWidget:
            self.mainWin.setCentralWidget(self.dockWidget);
        else:
            self.mainWin.addDockWidget(self.dockWidget.myLocArea, 
                                      self.dockWidget);
        self.dockWidget.qWidget.setVisible(True);
        self.dockWidget.bUndocked = False;


class MyTitleLbl(QtGui.QLabel):
    def __init__(self, text="", parent=None):
        QtGui.QLabel.__init__(self, text);
        self.parentDock = parent;
        
    def mouseDoubleClickEvent(self, event):
        super(MyTitleLbl, self).mouseDoubleClickEvent(event);
        self.parentDock.onBtnMaximize();
        
        
class MyDockWidget(QtGui.QDockWidget):
    def __init__(self, title="", parent=None, bCentralWidget=False):
        QtGui.QDockWidget.__init__(self, title, parent.mainWin);
        self.mainWin = parent.mainWin;
        self.parent = parent;
        self.bCentralWidget = bCentralWidget;
        self.createDockTitle(title);
        self.undockedWin = UndockedWin(self, parent);
        self.createArrayDockWidgets();
        
    def createArrayDockWidgets(self):
        try:
            self.parent.all_DockWidgets; #If (Exists aDockWidgets?)
        except:
            self.parent.all_DockWidgets = []; #Else If (NOT Exists aDockWidgets?)
        self.parent.all_DockWidgets.append(self);
        self.aDockWidgets = self.parent.all_DockWidgets;
        self.aDockIsVisible = [];
        for dockWidget in self.aDockWidgets:
            self.aDockIsVisible.append(dockWidget.isVisible());
        
    def createDockTitle(self, title):
        if self.bCentralWidget:
            self.setMovable(False);
        else:
            self.setMovable(True);
        self.btnUnDock = QtGui.QPushButton("");
        self.btnUnDock.setIcon(QtGui.QIcon("Images/Undock.gif"));
        self.btnMaximize = QtGui.QPushButton("");
        self.btnMaximize.setIcon(QtGui.QIcon("../../Images/application-resize-full.png"));
        self.btnClose = QtGui.QPushButton("");
        self.btnClose.setIcon(QtGui.QIcon("Images/Close.gif"));
        nS = 18;
        self.btnUnDock.setIconSize(QtCore.QSize(nS,nS));
        self.btnMaximize.setIconSize(QtCore.QSize(nS,nS));
        self.btnClose.setIconSize(QtCore.QSize(nS,nS));
        self.btnUnDock.setFlat(True);
        self.btnMaximize.setFlat(True);
        self.btnClose.setFlat(True);
        self.btnUnDock.setMaximumSize(nS,nS);
        self.btnMaximize.setMaximumSize(nS,nS);
        self.btnMaximize.setVisible(False);
        self.btnClose.setMaximumSize(nS,nS);
        self.lblTitle = MyTitleLbl(title, self);
        self.btnUnDock.clicked.connect(self.onBtnNewWin);
        self.btnMaximize.clicked.connect(self.onBtnMaximize);
        self.btnClose.clicked.connect(self.onBtnClose);
        self.dockLocationChanged.connect(self.newDockLocation);
        self.h_box = QtGui.QHBoxLayout();
        self.h_box.addWidget(self.lblTitle);
        self.h_box.addWidget(self.btnUnDock);
        self.h_box.addWidget(self.btnMaximize);
        self.h_box.addWidget(self.btnClose);
        self.h_box.setSpacing(0);
        self.h_box.setMargin(0);
        self.h_box.setContentsMargins(4,0,0,0);
        self.qWidget = QtGui.QWidget();
        self.qWidget.setLayout(self.h_box);
        self.qWidget.setMaximumHeight(50);
        self.qWidget.setStyleSheet(".QWidget {background-color: rgb(219,219,219);"+\
                                   " border-width: 1px; border-style: solid;"+\
                                   " border-color: rgb(185,185,185); }");
        self.setTitleBarWidget(self.qWidget);
        self.bMaximized = False;
        self.bUndocked = False;
        self.myLocArea = 0;
        
    def newDockLocation(self, area):
        self.myLocArea = area;
        
    def getDockLocation(self):
        return self.myLocArea;
    
    def setDockLocation(self, area):
        if self.bCentralWidget:
            self.mainWin.setCentralWidget(self);
        else:
            self.mainWin.addDockWidget(area, self);
        self.myLocArea = area;        
        
    def getDockMaximized(self):
        return self.bMaximized;

    def onBtnNewWin(self):
        self.undockedWin.setCentralWidget(self);
        self.undockedWin.show();
        self.qWidget.setVisible(False);
        self.undockedWin.setWindowTitle(self.lblTitle.text());
        
    def restoreDocked(self):
        self.undockedWin.close();

    def getUndocked(self):
        return self.bUndocked;

    def onBtnClose(self):
        self.setVisible(False);        

    def onBtnMaximize(self):
        if self.bMaximized == False:
            self.maximizeDock();
        else:
            self.minimizeDock();
            
    def setMovable(self, bMovable):
        if bMovable:
            self.setFeatures(QtGui.QDockWidget.DockWidgetClosable |
                             QtGui.QDockWidget.DockWidgetMovable);
        else:
            self.setFeatures(QtGui.QDockWidget.DockWidgetClosable);            
        
    def maximizeDock(self):
        #Set this as CentralWidget and move any current CentralWidget
        if not self.bCentralWidget:
            self.centralWidget = self.mainWin.centralWidget();
            if self.centralWidget != None:
                self.mainWin.addDockWidget(self.myLocArea, self.centralWidget);
                self.centralWidget.setMovable(True);
            self.mainWin.setCentralWidget(self);
            self.setMovable(False);
        #Hide all other Dock Widgets
        self.aDockIsVisible = [];
        for dockWidget in self.aDockWidgets:
            self.aDockIsVisible.append(dockWidget.isVisible());
            if dockWidget != self and not dockWidget.getUndocked():
                dockWidget.setEnabled(False);
                dockWidget.setVisible(False);
        self.btnClose.setEnabled(False);
        self.setFeatures(QtGui.QDockWidget.DockWidgetClosable);
        self.bMaximized = True;
    
    def minimizeDock(self):
        #Restore location of this DockWidget to his original area
        if not self.bCentralWidget:
            self.mainWin.addDockWidget(self.myLocArea, self);
            if self.centralWidget != None:
                self.mainWin.setCentralWidget(self.centralWidget);
                self.centralWidget.setMovable(False);
            self.setMovable(True);
        #Restore all other Dock Widgets Visibility
        for i in range(len(self.aDockWidgets)):
            if self.aDockWidgets[i] != self:
                self.aDockWidgets[i].setEnabled(True); 
                self.aDockWidgets[i].setVisible(self.aDockIsVisible[i]);
        self.btnClose.setEnabled(True);
        self.bMaximized = False;
